<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\Admin\RoleRequest;
use App\Services\Admin\RoleService;

class RoleController extends Controller
{

    public function options()
    {

        $data = RoleService::options();

        return $this->success(data: $data);
    }

    public function menuIds(int $id)
    {

        $data = RoleService::menuIds($id);

        return $this->success(data: $data);
    }

    public function menus(int $id, Request $request)
    {

        $validated = $request->validate([
            'menus' => 'nullable|array',
            'menus.*' => 'integer|min:1'
        ]);

        $meumIds = $validated['menus'] ?? [];

        $data = RoleService::menus($id, $meumIds);

        return $this->success(data: $data);
    }

    /**
     * 显示资源列表。
     */
    public function index(Request $request)
    {

        $params = $request->validate([
            'keywords'  => 'nullable|string',
            'pageNum'   => 'nullable|integer|min:1',
            'pageSize'  => 'nullable|integer|min:5',
        ]);

        $params['pageNum'] = $params['pageNum'] ?? 1;
        $params['pageSize'] = $params['pageSize'] ?? 10;

        $data = RoleService::index($params);

        return $this->success(data: $data);
    }

    /**
     * 将新创建的资源存储到数据库。
     */
    public function store(RoleRequest $request)
    {
        $validated = $request->validated();

        $data = RoleService::store($validated);

        return $this->success(data: $data);
    }

    /**
     * 显示指定资源的详细信息。
     */
    public function show(string $id)
    {

        $data = RoleService::show($id);

        return $this->success(data: $data);
    }

    /**
     * 更新指定资源。
     */
    public function update(RoleRequest $request, string $id)
    {
        $validated = $request->validated();

        $data = RoleService::update($validated, $id);

        return $this->success(data: $data);
    }

    /**
     * 删除指定资源。
     */
    public function destroy(string $id)
    {
        $data = RoleService::destroy($id);

        return $this->success(data: $data);
    }
}
